package cn.ee.dfs.wx.controller;

import cn.ee.dfs.common.annotation.Log;
import cn.ee.dfs.common.utils.DfsUtil;
import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.controller.BaseController;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.exception.DfsException;
import cn.ee.dfs.wx.model.WxUser;
import cn.ee.dfs.wx.service.IWxuserService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 微信用户表 Controller
 *
 * @author TWJ
 * @date 2023-07-29 12:23:31
 */
@Slf4j
@Validated
@RequestMapping("/wx/api/user")
@Controller
public class WxuserController extends BaseController {

    @Autowired
    private IWxuserService wxuserService;

    @GetMapping(DfsConstant.VIEW_PREFIX + "model")
    private String wxuserIndex(){
        return DfsUtil.view("model/model");
    }

    @GetMapping("wxuser")
    @ResponseBody
    @RequiresPermissions("model:list")
    public DfsResponse getAllWxusers(WxUser wxuser) {
        return new DfsResponse().success().data(wxuserService.findWxusers(wxuser));
    }

    @GetMapping("wxuser/list")
    @ResponseBody
    @RequiresPermissions("model:list")
    public DfsResponse wxuserList(QueryRequest request, WxUser wxuser) {
        Map<String, Object> dataTable = getDataTable(this.wxuserService.findWxusers(request, wxuser));
        return new DfsResponse().success().data(dataTable);
    }

    @Log("新增Wxuser")
    @PostMapping("wxuser")
    @ResponseBody
    @RequiresPermissions("model:add")
    public DfsResponse addWxuser(@Valid WxUser wxuser) throws DfsException {
        try {
            this.wxuserService.createWxuser(wxuser);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "新增Wxuser失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("删除Wxuser")
    @GetMapping("wxuser/delete")
    @ResponseBody
    @RequiresPermissions("model:delete")
    public DfsResponse deleteWxuser(WxUser wxuser) throws DfsException {
        try {
            this.wxuserService.deleteWxuser(wxuser);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "删除Wxuser失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("修改Wxuser")
    @PostMapping("wxuser/update")
    @ResponseBody
    @RequiresPermissions("model:update")
    public DfsResponse updateWxuser(WxUser wxuser) throws DfsException {
        try {
            this.wxuserService.updateWxuser(wxuser);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "修改Wxuser失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @PostMapping("wxuser/excel")
    @ResponseBody
    @RequiresPermissions("model:export")
    public void export(QueryRequest queryRequest, WxUser wxuser, HttpServletResponse response) throws DfsException {
        try {
            List<WxUser> wxusers = this.wxuserService.findWxusers(queryRequest, wxuser).getRecords();
            ExcelKit.$Export(WxUser.class, response).downXlsx(wxusers, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }
}
